

using System.Text.RegularExpressions;

namespace CSharpPro._1103
{
    public class UserService : IUserService
    {
        private List<User> registeredUsers = new List<User>();

        public bool Register(string username, string password, string confirmPassword)
        {
            if (string.IsNullOrEmpty(username) || username.Length < 3 || username.Length > 15 || !Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
            {
                Console.WriteLine("用户名格式错误（3-15位字母、数字、下划线）！");
                return false;
            }
            foreach (var user in registeredUsers)
            {
                if (user.Username == username)
                {
                    Console.WriteLine("用户名已存在！");
                    return false;
                }
            }
            
            if (string.IsNullOrEmpty(password) || password.Length < 6 || password.Length > 20 || !Regex.IsMatch(password, @"^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).+$"))
            {
                Console.WriteLine("密码格式错误（6-20位，含大小写字母和数字）！");
                return false;
            }
            
            if (string.IsNullOrEmpty(confirmPassword) || password != confirmPassword)
            {
                Console.WriteLine("密码与确认密码不一致！");
                return false;
            }

            registeredUsers.Add(new User(username, password));
            Console.WriteLine("注册成功！");
            return true;
        }

        public bool Login(string username, string password)
        {
            if (string.IsNullOrEmpty(username))
            {
                Console.WriteLine("用户名不可为空！");
                return false;
            }
            if (string.IsNullOrEmpty(password))
            {
                Console.WriteLine("密码不可为空！");
                return false;
            }

            User user = registeredUsers.Find(u => u.Username == username);
            if (user == null)
            {
                Console.WriteLine("用户名不存在！");
                return false;
            }
            if (user.Password != password)
            {
                Console.WriteLine("密码错误！");
                return false;
            }

            Console.WriteLine("登录成功！进入系统首页（模拟）");
            return true;
        }

        public List<User> GetRegisteredUsers()
        {
            return registeredUsers;
        }
    }
}